package view.user;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDate;
import java.util.Calendar;
import javax.swing.*;
/*
 * Created by JFormDesigner on Tue May 28 15:28:18 HKT 2024
 */

/**
 * @author 11565
 */
public class TimePanel extends JPanel {
    private Mainframe frame;
    private LocalDate startTime1;
    private LocalDate endTime1;
    public TimePanel(Mainframe frame) {
        initComponents();
        initializeComboBoxes();
        this.frame = frame;
    }

    private void Confrim(ActionEvent e) {
        // TODO add your code here
        String year = Integer.toString((Integer) this.year.getSelectedItem());
        String month = Integer.toString((Integer) this.month.getSelectedItem());
        String day = Integer.toString((Integer) this.day.getSelectedItem());
        String year2 = Integer.toString((Integer) this.year2.getSelectedItem());
        String month2 = Integer.toString((Integer) this.month2.getSelectedItem());
        String day2 = Integer.toString((Integer) this.day2.getSelectedItem());
        startTime1 = LocalDate.of(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(day));
        endTime1 = LocalDate.of(Integer.parseInt(year2), Integer.parseInt(month2), Integer.parseInt(day2));
        if(isValidDate(startTime1,endTime1)){
            frame.showRoomType(startTime1,endTime1);
        }else
            frame.showTimePanel();


    }

    //判断startTime endTime是否合理
    public boolean isValidDate(LocalDate startTime,LocalDate endTime) {
        if (startTime.isAfter(endTime)) {
            JOptionPane.showMessageDialog(null, "请选择正确的日期");
            return false;
        }else if (startTime.isEqual(endTime)) {
            JOptionPane.showMessageDialog(null, "请选择正确的日期");
            return false;
        } else {
            return true;
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - Qiqq
        panel2 = new JPanel();
        panel3 = new JPanel();
        label1 = new JLabel();
        ConfrimButton = new JButton();
        panel1 = new JPanel();
        year = new JComboBox();
        month = new JComboBox();
        day = new JComboBox();
        label3 = new JLabel();
        panel4 = new JPanel();
        year2 = new JComboBox();
        month2 = new JComboBox();
        day2 = new JComboBox();
        label4 = new JLabel();

        //======== this ========
        setBorder(new javax.swing.border.CompoundBorder(new javax.swing.border.TitledBorder(new
        javax.swing.border.EmptyBorder(0,0,0,0), "JF\u006frmDes\u0069gner \u0045valua\u0074ion",javax
        .swing.border.TitledBorder.CENTER,javax.swing.border.TitledBorder.BOTTOM,new java
        .awt.Font("D\u0069alog",java.awt.Font.BOLD,12),java.awt
        .Color.red), getBorder())); addPropertyChangeListener(new java.beans.
        PropertyChangeListener(){@Override public void propertyChange(java.beans.PropertyChangeEvent e){if("\u0062order".
        equals(e.getPropertyName()))throw new RuntimeException();}});

        //======== panel2 ========
        {
            panel2.setLayout(new CardLayout());

            //======== panel3 ========
            {

                //---- label1 ----
                label1.setText("\u8bf7\u9009\u62e9\u65e5\u671f\uff1a");

                //---- ConfrimButton ----
                ConfrimButton.setText("\u786e\u8ba4");
                ConfrimButton.addActionListener(e -> Confrim(e));

                //======== panel1 ========
                {

                    //---- label3 ----
                    label3.setText("\u5165\u4f4f\u65e5\u671f");

                    GroupLayout panel1Layout = new GroupLayout(panel1);
                    panel1.setLayout(panel1Layout);
                    panel1Layout.setHorizontalGroup(
                        panel1Layout.createParallelGroup()
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(label3, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
                                .addGap(24, 24, 24)
                                .addComponent(year, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(month, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(day, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
                                .addContainerGap())
                    );
                    panel1Layout.setVerticalGroup(
                        panel1Layout.createParallelGroup()
                            .addGroup(panel1Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(panel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                    .addComponent(day, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(month, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(label3, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(year, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    );
                }

                //======== panel4 ========
                {

                    //---- label4 ----
                    label4.setText("\u9000\u623f\u65e5\u671f");

                    GroupLayout panel4Layout = new GroupLayout(panel4);
                    panel4.setLayout(panel4Layout);
                    panel4Layout.setHorizontalGroup(
                        panel4Layout.createParallelGroup()
                            .addGroup(panel4Layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(label4, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(year2, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(month2, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(day2, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
                                .addContainerGap())
                    );
                    panel4Layout.setVerticalGroup(
                        panel4Layout.createParallelGroup()
                            .addGroup(panel4Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(panel4Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                    .addComponent(day2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(month2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(year2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(label4, GroupLayout.PREFERRED_SIZE, 28, GroupLayout.PREFERRED_SIZE))
                                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    );
                }

                GroupLayout panel3Layout = new GroupLayout(panel3);
                panel3.setLayout(panel3Layout);
                panel3Layout.setHorizontalGroup(
                    panel3Layout.createParallelGroup()
                        .addGroup(panel3Layout.createSequentialGroup()
                            .addGap(48, 48, 48)
                            .addGroup(panel3Layout.createParallelGroup()
                                .addComponent(label1, GroupLayout.PREFERRED_SIZE, 167, GroupLayout.PREFERRED_SIZE)
                                .addGroup(panel3Layout.createSequentialGroup()
                                    .addGap(59, 59, 59)
                                    .addGroup(panel3Layout.createParallelGroup()
                                        .addComponent(panel4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                        .addComponent(panel1, GroupLayout.PREFERRED_SIZE, 308, GroupLayout.PREFERRED_SIZE))))
                            .addContainerGap(227, Short.MAX_VALUE))
                        .addGroup(GroupLayout.Alignment.TRAILING, panel3Layout.createSequentialGroup()
                            .addContainerGap(418, Short.MAX_VALUE)
                            .addComponent(ConfrimButton)
                            .addGap(146, 146, 146))
                );
                panel3Layout.setVerticalGroup(
                    panel3Layout.createParallelGroup()
                        .addGroup(panel3Layout.createSequentialGroup()
                            .addGap(57, 57, 57)
                            .addComponent(label1, GroupLayout.PREFERRED_SIZE, 50, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(panel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(panel4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addGap(69, 69, 69)
                            .addComponent(ConfrimButton)
                            .addContainerGap(109, Short.MAX_VALUE))
                );
            }
            panel2.add(panel3, "card1");
        }

        GroupLayout layout = new GroupLayout(this);
        setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup()
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(panel2, GroupLayout.PREFERRED_SIZE, 642, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(2, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup()
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(panel2, GroupLayout.PREFERRED_SIZE, 423, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(1, Short.MAX_VALUE))
        );
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - Qiqq
    private JPanel panel2;
    private JPanel panel3;
    private JLabel label1;
    private JButton ConfrimButton;
    private JPanel panel1;
    private JComboBox year;
    private JComboBox month;
    private JComboBox day;
    private JLabel label3;
    private JPanel panel4;
    private JComboBox year2;
    private JComboBox month2;
    private JComboBox day2;
    private JLabel label4;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
    public void initializeComboBoxes() {
        // 初始化年份下拉框内容
        year.removeAllItems();
        for (int i = 2024; i <= 2034; i++) {
            year.addItem(i);
            year2.addItem(i);
        }

        // 初始化月份下拉框内容
        month.removeAllItems();
        for (int i = 1; i <= 12; i++) {
            month.addItem(i);
            month2.addItem(i);
        }

        // 初始化日期下拉框内容，确保只显示入住日期之后的日期
        updateDaysWithCheckInDate();

        // 为年份和月份下拉框添加事件监听器
        ActionListener updateDaysActionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                updateDaysWithCheckInDate();
            }
        };

        year.addActionListener(updateDaysActionListener);
        month.addActionListener(updateDaysActionListener);
    }

    public void updateDaysWithCheckInDate() {
        if (year.getSelectedItem() == null || month.getSelectedItem() == null) {
            return;
        }
        int selectedYear = (int) year.getSelectedItem();
        int selectedMonth = (int) month.getSelectedItem();

        // 由于Calendar月份是从0开始的，所以在获取月份时需要减1
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, selectedYear);
        calendar.set(Calendar.MONTH, selectedMonth - 1);

        int maxDayOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        day.removeAllItems();
        // 只添加入住日期之后的日期
        for (int i = 1; i <= maxDayOfMonth; i++) {
            day.addItem(i);
            day2.addItem(i);
        }
    }
}
